unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, rtcDataSrv, rtcInfo, rtcConn, rtcHttpSrv,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    HtpSrv_1: TRtcHttpServer;
    RtcDataProvider1: TRtcDataProvider;
    Chkb_1: TCheckBox;
    procedure Chkb_1Click(Sender: TObject);
    procedure RtcDataProvider1CheckRequest(Sender: TRtcConnection);
    procedure RtcDataProvider1DataReceived(Sender: TRtcConnection);
  private
    FTargetName:string;
    FTargetPath:string;
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzDebug,Class_KzUtils;

{$R *.dfm}

procedure TForm1.Chkb_1Click(Sender: TObject);
begin
  if TCheckBox(Sender).Checked then
  begin
    with HtpSrv_1 do
    begin
      ServerAddr:='localhost';
      ServerPort:='9090';
      Listen();
    end;
  end else
  begin
    with HtpSrv_1 do
    begin
      StopListen;
    end;
  end;
end;

procedure TForm1.RtcDataProvider1CheckRequest(Sender: TRtcConnection);
var
  RtcDataServer:TRtcDataServer absolute Sender;
begin
  with RtcDataServer do
  begin
    if UpperCase(Request.FileName)='/UPLOAD' then
    begin
      Accept;
    end else
    if UpperCase(Request.FileName)='/OBTAIN' then
    begin
      Accept;
    end;
  end;
end;

procedure TForm1.RtcDataProvider1DataReceived(Sender: TRtcConnection);
var
  Value:string;
  RtcDataServer:TRtcDataServer absolute Sender;
begin
  with RtcDataServer do
  begin
    if UpperCase(Request.FileName)='/UPLOAD' then
    begin
      if Request.Started then
      begin
        if not DirectoryExists(TKzUtils.ExePath+'UPLOAD') then
        begin
          CreateDir(TKzUtils.ExePath+'UPLOAD');
        end;
        FTargetName:=URL_Decode(Request.Query.Value['UPLOAD_TARGET']);
        FTargetPath:=TKzUtils.ExePath+'UPLOAD\'+FTargetName;
      end;

      Value := Read;
      Write_File(FTargetPath,Value,Request.ContentIn - Length(Value));
    end else
    if UpperCase(Request.FileName)='/OBTAIN' then
    begin
      if Request.Started then
      begin
        FTargetName:=URL_Decode(Request.Query.Value['OBTAIN_TARGET']);
        FTargetPath:=TKzUtils.ExePath+'UPLOAD\'+FTargetName;
      end;

      if File_Exists(FTargetPath) then
      begin
        Value:=Read_File(FTargetPath);
        Write(Value);
      end;
    end;
  end;
end;


{var
  Value: string;
  TMPA : RtcString;
  RtcDataServer:TRtcDataServer absolute Sender;
begin
  //with TRtcDataServer(Sender) do
  with RtcDataServer do
  begin
    if UpperCase((Request.FileName)) = '/UPLOAD' then
    begin
      if Request.Started then
      begin
        TMPA:=ExtractFilepath(ParamStr(0)) + 'UPLOAD\'+ string(URL_Decode(Request.Query['UPLOAD_TARGET']));
        Request.Info['UPLOAD_TARGET']:=TMPA;

        if not DirectoryExists(ExtractFilepath(ParamStr(0)) + 'UPLOAD') then
        begin
          CreateDir(ExtractFilepath(ParamStr(0)) + 'UPLOAD');
        end;
        Delete_File(Request.Info['UPLOAD_TARGET']);
      end;
      TMPA:=Request.Info.asString['UPLOAD_TARGET'];

      Value := Read;
      Write_File(TMPA,Value,Request.ContentIn - Length(Value));
    end else
    if UpperCase(Request.FileName)='/OBTAIN' then
    begin
      if Request.Started then
      begin
        TMPA:=ExtractFilepath(ParamStr(0)) + 'UPLOAD\'+'demo.exe';
        Request.Info['OBTAIN_TARGET']:=TMPA;
      end;
      TMPA := Request.Info.asString['OBTAIN_TARGET'];
      if File_Exists(TMPA) then
      begin
        Value:=Read_File(TMPA);
        Write(Value);
      end;
    end;

    if Request.Complete then
    begin
      //Write;
    end;
  end;
end;}

end.
